#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtSvg import *

from calculation import *

import sys
import os
import feedparser

from main_ui import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        
        #Self.scene for qgraphicvie
        self.scene = QGraphicsScene(self)     
        self.ObjectView.setScene(self.scene)


        self.ObSquare = Square(0)
        self.ObCircle = Circle(0)
        self.ObRectangle = Rectangle(0,0)
        self.ObCube = Cube(0)
        self.ObSphere = Sphere(0)
        self.ObCylinder = Cylinder(0,0)
  
        self.ObRectPris = RectanPrism(0,0,0)
        #Main SIGNALS

        self.connect(self.tabWidget, SIGNAL('currentChanged(int)'), self.clean_up)
        self.connect(self.aboutBut, SIGNAL('clicked()'), self.about_Me)
        self.connect(self.webBut, SIGNAL('clicked()'), self.openMyWebPage)
        self.connect(self.updateBut, SIGNAL('clicked()'), self.showVersions)

        #Square SIGNALS

        self.connect(self.sqSpin, SIGNAL('valueChanged(int)'), self.create_Square)

        #Circle SIGNALS

        self.connect(self.ciSpin, SIGNAL('valueChanged(int)'), self.create_Circle)
 
        #Rectangle SIGNALS
        self.connect(self.recLength, SIGNAL('valueChanged(int)'), self.create_Rectangle)
        self.connect(self.recWidth, SIGNAL('valueChanged(int)'), self.create_Rectangle)

        #Cube SIGNALS


        self.connect(self.cubEdge, SIGNAL('valueChanged(int)'), self.create_Cube)

        #Sphere SIGNALS

        self.connect(self.sphRadius, SIGNAL('valueChanged(int)'), self.create_Sphere)        
        

        #Cylinder SIGNALS
   
        self.connect(self.cylRadius,SIGNAL('valueChanged(int)'), self.create_Cylinder)
        self.connect(self.cylHeight, SIGNAL('valueChanged(int)'), self.create_Cylinder)
  
          
        #Main values
        self.currentTabText = u'Kare'
        self.resultBrowser.append(u'Seçilen Şekil: ' + self.currentTabText)

    def showVersions(self):
        d = feedparser.parse( "http://code.google.com/feeds/p/gfh-araci/downloads/basic")
        versions = []
        x = 0
        while x < len(d.entries):
              current = d.entries[x]['title']
              versions.append(current)
              x = x + 1
        self.resultBrowser_3.clear()
        self.resultBrowser_3.append(u'Şu an ki indirilebilir sürümler:')
        for i in versions:
            self.resultBrowser_3.append('-> '+i)

    def about_Me(self):
        aboutText = """<p><font size=30><b>gfh-araci   0.2<b></b></font></p>

<p><b>Gelistiriciler:</b></p>

<p>Taha Dogan Gunes, tdgunes@gmail.com</p>
<p>Okan Okurogullari, ookurogullari@gmail.com</p>

<p><b>Lisans:</b> GPLv3</p>

<p><b>Web Site:</b></p>

<p>http://code.google.com/p/gfh-araci</p>


"""
        QMessageBox.about(self, u"Hakkında - GFH-Araci", aboutText)

    def openMyWebPage(self):
        QDesktopServices.openUrl(QUrl("http://code.google.com/p/gfh-araci"))
    def clean_up(self):
        #Cleaning resultbrowsers
        self.resultBrowser.clear()
        self.resultBrowser_2.clear()
        #Square 
        self.sqSpin.setValue(0)
        #Circle
        self.ciSpin.setValue(0)
        #Rectangle
        self.recLength.setValue(0)
        self.recWidth.setValue(0)
        #Cube
        self.cubEdge.setValue(0)
        #Sphere
        self.sphRadius.setValue(0)
        #Clyinder
        self.cylHeight.setValue(0)
        self.cylRadius.setValue(0)
        #Cone
        #Rectangular Prism
        #Show the selected tab
        tabs = [u'Kare', u'Daire', u'Dikdörtgen', u'Küp', u'Küre', u'Silindir','Koni','-']
        self.currentTabText = tabs[self.tabWidget.currentIndex()]
        self.resultBrowser.append(u'Seçilen Cisim: ' + self.currentTabText)
   #SQUARE
    def create_Square(self):
        self.scene.clear()
        self.resultBrowser_2.clear()
        edge = self.sqSpin.value()
        self.ObSquare.a = edge
        peripheryText = u'Çevresi:' + '  ' +str(self.ObSquare.periphery())
        areaText = u'Alanı:' + ' ' + str(self.ObSquare.area())
        self.resultBrowser_2.append(peripheryText)
        self.resultBrowser_2.append(areaText)
        fileWrite = open('draft.svg','w')
        fileWrite.write(self.ObSquare.return_SVG())
        fileWrite.close()
        svg_Item=QGraphicsSvgItem('draft.svg')
        #svg_Item.setPos(300,44)
        self.scene.addItem(svg_Item)   
        os.remove('draft.svg')

    def create_Circle(self):
        self.scene.clear()
        self.resultBrowser_2.clear()
        radius = self.ciSpin.value()
        self.ObCircle.a = radius
        peripheryText = u'Çevresi:' + '  ' +str(self.ObCircle.periphery())
        areaText = u'Alanı:' + ' ' + str(self.ObCircle.area())
        self.resultBrowser_2.append(peripheryText)
        self.resultBrowser_2.append(areaText)
        fileWrite = open('draft.svg','w')
        fileWrite.write(self.ObCircle.return_SVG())
        fileWrite.close()
        svg_Item=QGraphicsSvgItem('draft.svg')
        #svg_Item.setPos(300,44)
        self.scene.addItem(svg_Item)   
        os.remove('draft.svg')
    
    def create_Rectangle(self):
        self.scene.clear()
        self.resultBrowser_2.clear()
        self.ObRectangle.l = self.recLength.value()
        self.ObRectangle.b = self.recWidth.value()
        peripheryText = u'Çevresi:' + '  ' +str(self.ObRectangle.periphery())
        areaText = u'Alanı:' + ' ' + str(self.ObRectangle.area())
        self.resultBrowser_2.append(peripheryText)
        self.resultBrowser_2.append(areaText)
        fileWrite = open('draft.svg','w')
        fileWrite.write(self.ObRectangle.return_SVG())
        fileWrite.close()
        svg_Item=QGraphicsSvgItem('draft.svg')
        #svg_Item.setPos(300,44)
        self.scene.addItem(svg_Item)   
        os.remove('draft.svg')

 

    def create_Cube(self):
        self.scene.clear()
        self.resultBrowser_2.clear()
        edge = self.cubEdge.value()
        self.ObCube.a = edge
        message = [u'Kesit Alanı:' + '  ' +str(self.ObCube.CroSecArea()),
                   u'Yüzel Alanı:' + '  ' +str(self.ObCube.SurfArea()),
                   u'Hacmi:' + '  ' +str(self.ObCube.Volume()),
                   u'Kesit Alanı/Hacim :' + '  ' +str(self.ObCube.CroSecAreaDivVol()),
                   u'Yüzey Alanı/Hacim :' + '  ' +str(self.ObCube.SurfAreaDivVol())]
        for i in message:
            self.resultBrowser_2.append(i)
        fileWrite = open('draft.svg','w')
        fileWrite.write(self.ObCube.return_SVG())
        fileWrite.close()
        svg_Item=QGraphicsSvgItem('draft.svg')
        #svg_Item.setPos(300,44)
        self.scene.addItem(svg_Item)   
        os.remove('draft.svg')


    def create_Sphere(self):
        self.scene.clear()
        self.resultBrowser_2.clear()
        radius = self.sphRadius.value()
        self.ObSphere.a = radius
        message = [u'Kesit Alanı:' + '  ' +str(self.ObSphere.CroSecArea()),
                   u'Yüzey Alanı:' + '  ' +str(self.ObSphere.SurfArea()),
                   u'Hacim:' + '  ' +str(self.ObSphere.Volume()),
                   u'Kesit Alanı/Hacim:' + '  ' +str(self.ObSphere.CroSecAreaDivVol()),
                   u'Yüzey Alanı/Hacim :' + '  ' +str(self.ObSphere.SurfAreaDivVol())]
        for i in message:
            self.resultBrowser_2.append(i)
        fileWrite = open('draft.svg','w')
        fileWrite.write(self.ObSphere.return_SVG())
        fileWrite.close()
        svg_Item=QGraphicsSvgItem('draft.svg')
        #svg_Item.setPos(300,44)
        self.scene.addItem(svg_Item)   
        os.remove('draft.svg')
    
    def create_Cylinder(self):
        self.scene.clear()
        self.resultBrowser_2.clear()
        radius = self.cylRadius.value()
        height = self.cylHeight.value()
        self.ObCylinder.a = radius
        self.ObCylinder.h = height
        message = [u'Kesit Alanı:' + '  ' +str(self.ObCylinder.CroSecArea()),
                   u'Yüzey Alanı:' + '  ' +str(self.ObCylinder.SurfArea()),
                   u'Hacim:' + '  ' +str(self.ObCylinder.Volume()),
                   u'Kesit Alanı/Hacim:' + '  ' +str(self.ObCylinder.CroSecAreaDivVol()),
                   u'Yüzey Alanı/Hacim:' + '  ' + str(self.ObCylinder.SurfAreaDivVol())]
        for i in message:
            self.resultBrowser_2.append(i)
        fileWrite = open('draft.svg','w')
        fileWrite.write(self.ObCylinder.return_SVG())
        fileWrite.close()
        svg_Item=QGraphicsSvgItem('draft.svg')
        #svg_Item.setPos(300,44)
        self.scene.addItem(svg_Item)   
        os.remove('draft.svg')


